package com.opgea.crm.domain.entities.security;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;


import com.opgea.crm.domain.entities.Employee;

@Entity
@Table(name="users")
public class CrmUser implements Serializable{


	private static final long serialVersionUID = 1L;
	
	@Id
	//@GeneratedValue
	@Column(name="username")
	private String userName;
	@Column(name="password")
	private String password;
	@Column(name="enabled")
	private Boolean enabled;
	@Column(name="email")
	private String email;
	
	/*
	@ManyToOne(cascade=CascadeType.REFRESH)
	@JoinColumn(name="role_id", referencedColumnName="id")
	private Role role;
	*/
	
	@ManyToMany(cascade=CascadeType.REFRESH)
	@JoinTable(name="user_group_crm",
				joinColumns={@JoinColumn(name="users_username")},
			    inverseJoinColumns={@JoinColumn(name="groups_id")})
	private List<Group> groups;
	
	@OneToOne(cascade=CascadeType.REFRESH)
	@JoinColumn(name="emp_id", referencedColumnName="id")
	private Employee employee;

	
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Boolean getEnabled() {
		return enabled;
	}
	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getUserName() {
		return userName;
	}
	
	/*
	public void setRole(Role role) {
		this.role = role;
	}
	public Role getRole() {
		return role;
	}
	*/
	
        /*
	public static Authentication getCurrentAuthentication() {
		return SecurityContextHolder.getContext().getAuthentication();
	}
	
	public static CurrentUser getCurrentUser() {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		return (CurrentUser) auth.getPrincipal();
	}
	*/
	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}
	public List<Group> getGroups() {
		return groups;
	}
	

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	public Employee getEmployee() {
		return employee;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((userName == null) ? 0 : userName.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CrmUser other = (CrmUser) obj;
		if (userName == null) {
			if (other.userName != null)
				return false;
		} else if (!userName.equals(other.userName))
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		return "CrmUser [userName=" + userName + ", password="
				+ password + ", enabled=" + enabled + ", email=" + email + "]";
	}
	
	
	
}
